// app.js
App({
  onLaunch() {
    // 初始化单词数据
    this.initWordData()
    
    // 展示本地存储能力
    const logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)

    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
      }
    })
  },
  
  // 初始化单词数据
  initWordData() {
    const wordData = wx.getStorageSync('wordData')
    if (!wordData) {
      // 预设一些常用单词
      const defaultWords = [
        { word: 'apple', translation: '苹果', phonetic: '/ˈæpl/', difficulty: 1, learned: false, reviewCount: 0 },
        { word: 'book', translation: '书', phonetic: '/bʊk/', difficulty: 1, learned: false, reviewCount: 0 },
        { word: 'cat', translation: '猫', phonetic: '/kæt/', difficulty: 1, learned: false, reviewCount: 0 },
        { word: 'dog', translation: '狗', phonetic: '/dɔːɡ/', difficulty: 1, learned: false, reviewCount: 0 },
        { word: 'elephant', translation: '大象', phonetic: '/ˈelɪfənt/', difficulty: 2, learned: false, reviewCount: 0 },
        { word: 'flower', translation: '花', phonetic: '/ˈflaʊə(r)/', difficulty: 2, learned: false, reviewCount: 0 },
        { word: 'garden', translation: '花园', phonetic: '/ˈɡɑːdn/', difficulty: 2, learned: false, reviewCount: 0 },
        { word: 'house', translation: '房子', phonetic: '/haʊs/', difficulty: 1, learned: false, reviewCount: 0 },
        { word: 'internet', translation: '互联网', phonetic: '/ˈɪntənet/', difficulty: 3, learned: false, reviewCount: 0 },
        { word: 'journey', translation: '旅程', phonetic: '/ˈdʒɜːni/', difficulty: 3, learned: false, reviewCount: 0 }
      ]
      wx.setStorageSync('wordData', defaultWords)
    }
    
    // 初始化学习进度
    const studyProgress = wx.getStorageSync('studyProgress')
    if (!studyProgress) {
      wx.setStorageSync('studyProgress', {
        totalWords: 0,
        learnedWords: 0,
        todayLearned: 0,
        streak: 0,
        lastStudyDate: null
      })
    }
  },
  
  globalData: {
    userInfo: null,
    currentWordIndex: 0,
    studyMode: 'learn' // 'learn', 'review', 'test'
  }
})
